package org.hcf.utils.excel.poi.base.exp.header;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.hcf.utils.lang.StringUtils;

import java.util.Collection;
import java.util.Objects;
import java.util.function.Function;

/**
 * @author hecf
 * @date 2021/9/4
 */
public class StrListTableHeader<T> extends TableHeader<T> {

    private Function<T, Collection<String>> getStrListFunc;

    public StrListTableHeader(String fieldName, int width, Function<T, Collection<String>> getStrListFunc) {
        super(fieldName, width);
        this.getStrListFunc = getStrListFunc;
    }

    @Override
    public void dataCellout(Cell cell, T t) {
        Collection<String> strList = null;
        if(Objects.nonNull(getStrListFunc)){
            strList = getStrListFunc.apply(t);
        }
        String str = null;
        if(CollectionUtils.isNotEmpty(strList)){
            str = StringUtils.join(strList,",");
        }else {
            str = "";
        }
        cell.setCellValue(str);
    }
}
